﻿package com.ylkj.timer.service.impl;

import java.util.List;

import com.ylkj.timer.dao.CommonDao;
import com.ylkj.timer.service.CommonService;




/**
 * 公共服务�?
 * @author LinJianPing
 */
public class CommonServiceImpl implements CommonService{	
			
	private CommonDao commonDao;	
	

	/**
	 * 删除记录
	 * @param nameSpace 命名空间
	 * @param sqlId SQL语句ID
	 * @param param 参数对象
	 */
	public int delete(String nameSpace, String sqlId, Object param) throws Exception{
		return this.commonDao.delete(nameSpace, sqlId, param);
	}
	
	/**
	 * 新增记录
	 * @param nameSpace 命名空间
	 * @param sqlId SQL语句ID
	 * @param param 参数对象
	 */
	public int insert(String nameSpace, String sqlId, Object param) throws Exception {
		Object object = this.commonDao.insert(nameSpace, sqlId, param);
		if(object==null)
			return 0;
		else
			return Integer.parseInt(String.valueOf(object));
	}
	
	/**
	 * 新增记录
	 * @param nameSpace 命名空间
	 * @param sqlId SQL语句ID
	 * @param paramList 参数对象列表
	 */
	public int insert(String nameSpace, String sqlId, List<Object> paramList) throws Exception {
		if(paramList==null)
			throw new RuntimeException("参数列表为空"); 
		int count = 0;
		for(Object param : paramList){
			count += this.insert(nameSpace, sqlId, param);
		}
		return count;
	}
	
	/**
	 * 更新记录
	 * @param nameSpace 命名空间
	 * @param sqlId SQL语句ID
	 * @param param 参数对象
	 */
	public int update(String nameSpace, String sqlId, Object param) throws Exception {
		return this.commonDao.update(nameSpace, sqlId, param);
	}
	
	/**
	 * 更新记录
	 * @param nameSpace 命名空间
	 * @param sqlId SQL语句ID
	 * @param paramList 参数对象列表
	 */
	public int update(String nameSpace, String sqlId, List<Object> paramList) throws Exception {
		if(paramList==null)
			throw new RuntimeException("参数列表为空"); 
		int count = 0;
		for(Object param : paramList){
			count += this.update(nameSpace, sqlId, param);
		}
		return count;
	}
	

	
	/**
	 * 查询记录
	 * @param nameSpace 命名空间
	 * @param sqlId SQL语句ID
	 * @param param 参数对象
	 */
	@SuppressWarnings("unchecked")
	public Object queryForObject(String nameSpace, String sqlId, Object param) throws Exception {
		List dataList = this.commonDao.find(nameSpace, sqlId, param);
		if(dataList==null || dataList.size()==0)
			return null;
		else
			return dataList.get(0);
	}
	
	/**
	 * 查询记录
	 * @param nameSpace 命名空间
	 * @param sqlId SQL语句ID
	 * @param param 参数对象
	 */
	@SuppressWarnings("unchecked")
	public List queryForList(String nameSpace, String sqlId, Object param) throws Exception {
		return this.commonDao.find(nameSpace, sqlId, param);
	}
	

	public CommonDao getCommonDao() {
		return commonDao;
	}
	
	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}
	
}
